//
//  STwoDimensionCode.m
//  LSCodeLibrary
//
//  Created by Samuel on 15/4/2.
//  Copyright (c) 2015年 Samuel. All rights reserved.
//

#import "STwoDimensionCode.h"

@implementation STwoDimensionCode
#pragma mark 二维码生成string为传人字符串
+(UIImage *)CreatTwoDimensionCode:(NSString *)string
{
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKey:@"inputMessage"];
    CIImage *outputImage = [filter outputImage];
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgImage = [context createCGImage:outputImage
                                       fromRect:[outputImage extent]];
    UIImage *image = [UIImage imageWithCGImage:cgImage
                                         scale:1.
                                   orientation:UIImageOrientationUp];
//    // Resize without interpolating
//    UIImage *resized = [self resizeImage:image
//                             withQuality:kCGInterpolationNone
//                                    rate:5.0];
    CGImageRelease(cgImage);
    return image;
}
#pragma mark - imagePrivate(默认不改变)
+ (UIImage*)resizeImageDefault:(UIImage *)image
                          rate:(CGFloat)rate
{
    UIImage *resized = [self resizeImage:image
                             withQuality:kCGInterpolationNone
                                    rate:rate];
    return resized;
}



#pragma mark - imagePrivate放大二维码
+ (UIImage *)resizeImage:(UIImage *)image
             withQuality:(CGInterpolationQuality)quality
                    rate:(CGFloat)rate
{
    UIImage *resized = nil;
    CGFloat width = image.size.width * rate;
    CGFloat height = image.size.height * rate;
    
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, quality);
    [image drawInRect:CGRectMake(0, 0, width, height)];
    resized = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resized;
}

@end
